package batframework.mvc;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("unchecked")
public class ModelMap
{
	private final Map<String, Object> attr = new LinkedHashMap<String,Object>();
	
	/**
	 * 
	 * @author bmyues
	 * @param name
	 * @param value
	 * @return
	 */
	public ModelMap addAttr(String name,Object value)
	{
		attr.put(name, value);
		return this;
	}
	
	/**
	 * 
	 * @author bmyues
	 * @param name
	 * @return
	 */
	public <T> T getAttr(String name)
	{
		return (T) attr.get(name);
	}
	
	/**
	 * 
	 * @author bmyues
	 * @param attrMap
	 * @return
	 */
	public ModelMap addAttrs(Map<String, Object> attrMap)
	{
		for (Map.Entry<String, Object> en : attrMap.entrySet())
			attr.put(en.getKey(), en.getValue());
		return this;
	}
	
	/**
	 * 
	 * @author bmyues
	 * @param name
	 * @return
	 */
	public ModelMap removeAttr(String name)
	{
		attr.remove(name);
		return this;
	}

	/**
	 * 
	 * @author bmyues
	 * @return
	 */
	public Map<String, Object> getAttrs()
	{
		return this.attr; 
	}
	
	/**
	 * model 
	 * @author bmyues
	 * @return
	 */
	public String getQueryString()
	{
		if(attr.size() == 0)
			return "";
		StringBuilder sb = new StringBuilder("?");
		for (Entry<String, Object> en : attr.entrySet())
			sb.append(en.getKey()).append("=").append(en.getValue()).append("&");
		String queryString = sb.toString();
		return queryString.substring(0, queryString.length()  - 1);
	}

}
